'''
多层感知机
'''

import scipy.io as scio
import datetime
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2


# 读取mat文件
def readMat(matPath):
    return scio.loadmat(matPath)

# 加载数据集
matPath='dataSet20180402.mat'
dataSet=readMat(matPath)
print('数据集读取完成')

# 读取数据集
test=np.array(dataSet['test'])
testX=np.array(dataSet['testX'])
testY=np.array(dataSet['testY']).T[0]
train=np.array(dataSet['train'])
trainX=np.array(dataSet['trainX'])
trainY=np.array(dataSet['trainY']).T[0]
print('数据集读取完成')

# 数据集集成
dataX=np.concatenate((trainX,testX))
dataY=np.concatenate((trainY,testY))
result=SelectKBest(chi2, k=100).fit_transform(dataX, dataY)

# 重新划分数据集
trainXNew=result[0:len(trainX)]
testXNew=result[len(trainX):]
trainNew=np.hstack((trainXNew,dataSet['trainY']))
testNew=np.hstack((testXNew,dataSet['testY']))

matNewPath='dataSet20180403-K100.mat'
scio.savemat(matNewPath,{
    'test':testNew,
    'train':trainNew,
    'testX':testXNew,
    'testY':dataSet['testY'],
    'trainX':trainXNew,
    'trainY':dataSet['trainY']
})

# ch,pval=chi2(dataX,dataY)
# ch[np.argpartition(ch,-150)[-150:]]
# print(np.sort(pval))
